package com.ybt.heartratem.logic.data.repositor;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ybt.heartratem.common.entity.ClassInfo;
import com.ybt.heartratem.common.entity.StudentInfo;
import com.ybt.heartratem.common.entity.TeacherInfo;
import com.ybt.heartratem.logic.data.http.AASDataSource;
import com.ybt.heartratem.logic.data.http.service.entry.AASRequestBody;
import com.ybt.heartratem.logic.data.local.LocalDataSource;

import java.util.List;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.UploadParam;
import okhttp3.ResponseBody;

/**
 * MVVM的Model层，统一模块的数据仓库
 * 包含网络数据和本地数据（一个应用可以有多个Repositor）
 */
public class AASDataRepository extends BaseModel implements AASDataSource, LocalDataSource {

    private volatile static AASDataRepository INSTANCE = null;
    private final AASDataSource mHttpDataSource;
    private final LocalDataSource mLocalDataSource;

    private AASDataRepository(@NonNull AASDataSource httpDataSource, @NonNull LocalDataSource localDataSource) {
        this.mHttpDataSource = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static AASDataRepository getInstance(AASDataSource httpDataSource, LocalDataSource localDataSource) {
        if (INSTANCE == null) {
            synchronized (AASDataRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AASDataRepository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }


    @Override
    public void saveInitPage(boolean flag) {
        mLocalDataSource.saveInitPage(flag);
    }

    @Override
    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    @Override
    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    @Override
    public void saveToken(String token) {
        mLocalDataSource.saveToken(token);
    }

    @Override
    public void saveConfigInfo(String config) {
        mLocalDataSource.saveConfigInfo(config);
    }

    @Override
    public void saveLoginUser(String loginUser) {
        mLocalDataSource.saveLoginUser(loginUser);
    }

    @Override
    public void saveIsQueryLocalDB(String key, boolean value) {
        mLocalDataSource.saveIsQueryLocalDB(key, value);
    }

    @Override
    public boolean getInitPage() {
        return mLocalDataSource.getInitPage();
    }

    @Override
    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    @Override
    public String getPassword() {
        return mLocalDataSource.getPassword();
    }

    @Override
    public String getToken() {
        return mLocalDataSource.getToken();
    }

    @Override
    public String getConfigInfo() {
        return mLocalDataSource.getConfigInfo();
    }

    @Override
    public String getLoginUser() {
        return mLocalDataSource.getLoginUser();
    }

    @Override
    public boolean getQueryLocalDB(String key) {
        return mLocalDataSource.getQueryLocalDB(key);
    }

    @Override
    public Observable<BaseResponse<List<ClassInfo>>> getClassList(String SchoolID) {
        return mHttpDataSource.getClassList(SchoolID);
    }

    @Override
    public Observable<BaseResponse<List<TeacherInfo>>> GetEmployeeList(String SchoolID) {
        return mHttpDataSource.GetEmployeeList(SchoolID);
    }

    @Override
    public Observable<BaseResponse<List<StudentInfo>>> getStudentList(String SchoolID, int ClassID) {
        return mHttpDataSource.getStudentList(SchoolID, ClassID);
    }

    @Override
    public Observable<UploadParam> getOssSignData(String FileName) {
        return mHttpDataSource.getOssSignData(FileName);
    }

    @Override
    public Observable<JsonObject> getRecordList(int schoolID, int ModelType, int PageIndex, int PageSize) {
        return mHttpDataSource.getRecordList(schoolID, ModelType, PageIndex, PageSize);
    }

    @Override
    public Observable<JsonObject> getRecordByID(int ID) {
        return mHttpDataSource.getRecordByID(ID);
    }

    @Override
    public Observable<ResponseBody> AddRecord(AASRequestBody body) {
        return mHttpDataSource.AddRecord(body);
    }

    @Override
    public Observable<ResponseBody> UpdateRecord(AASRequestBody body) {
        return mHttpDataSource.UpdateRecord(body);
    }

    @Override
    public Observable<ResponseBody> DeleteRecord(int ID) {
        return mHttpDataSource.DeleteRecord(ID);
    }

    @Override
    public Observable<BaseResponse<List<TeacherInfo>>> GetEmployeeList(String SchoolID, int RoleID) {
        return mHttpDataSource.GetEmployeeList(SchoolID, RoleID);
    }

    @Override
    public Observable<JsonArray> getFoodBook(String SchoolID, String TypeOf, String DateFrom, String DateTo) {
        return mHttpDataSource.getFoodBook(SchoolID, TypeOf, DateFrom, DateTo);
    }

    @Override
    public Observable<JsonObject> getSchoolInfo(int SchoolID) {
        return mHttpDataSource.getSchoolInfo(SchoolID);
    }

}
